﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ConsoleWindow : EditorWindow
{
    private Rect topArea;
    private Rect bottomArea;
    private float splitHeight = 0.5f;
    private float minHeight = 50f;
    private bool draggingSplit = false;

    private static ConsoleWindow window;
    [MenuItem("Tools/LogManager/ConsoleWindow1")]
    public static void ShowConsoleWindow()
    {
        window = GetWindow<ConsoleWindow>();
        window.Show();
    }
    private Vector2 scrollPos;
    private void OnGUI()
    {
        // 计算上下两部分的区域
        topArea = new Rect(0, 0, position.width, splitHeight * position.height);
        bottomArea = new Rect(0, topArea.height, position.width, (1 - splitHeight) * position.height);

        // 绘制上下两部分的内容
        GUILayout.BeginArea(topArea, new GUIStyle("FrameBox"));
        ToolBar();
        GUILayout.Space(10);
        MessageScroll();
        GUILayout.EndArea();

        GUILayout.BeginArea(bottomArea, new GUIStyle("FrameBox"));
        GUILayout.Label("堆栈信息\n堆栈信息");
        GUILayout.EndArea();

        //鼠标拖拽
        MouseDrag();
    }

    private void MouseDrag()
    {
        //鼠标拖拽区域
        EditorGUIUtility.AddCursorRect(new Rect(0, topArea.height - 2, position.width, 6), MouseCursor.ResizeVertical);
        // 检测鼠标事件
        Event e = Event.current;
        switch (e.type)
        {
            case EventType.MouseDown:
                if (e.button == 0 && new Rect(0, topArea.height - 2, position.width, 4).Contains(e.mousePosition))
                {
                    draggingSplit = true;
                    GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
                }
                break;
            case EventType.MouseDrag:
                if (draggingSplit)
                {
                    splitHeight += e.delta.y / position.height;
                    splitHeight = Mathf.Clamp(splitHeight, minHeight / position.height, 1 - minHeight / position.height);
                    e.Use();
                }
                break;
            case EventType.MouseUp:
                if (draggingSplit && e.button == 0)
                {
                    draggingSplit = false;
                    GUIUtility.hotControl = 0;
                    e.Use();
                }
                break;
        }
    }

    private void ToolBar()
    {
        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        ClearBtn();
        CollapseBtn();
        ErrorPauseBtn();
        SearchField();
        GUILayout.EndHorizontal();
    }

    private void MessageScroll()
    {
        scrollPos = GUILayout.BeginScrollView(scrollPos);
        for (int i = 0; i < 10; i++)
        {
            GUILayout.Label(i.ToString());
        }
        GUILayout.EndScrollView();
    }

    private void ClearBtn()
    {
        if (GUILayout.Button("清理",EditorStyles.toolbarDropDown))
        {

        }
    }

    private void CollapseBtn()
    {
        if (GUILayout.Button("折叠",EditorStyles.toolbarButton))
        {

        }
    }

    private void ErrorPauseBtn()
    {
        if (GUILayout.Button("暂停",EditorStyles.toolbarButton))
        {

        }
    }
    private string searchStr;
    private void SearchField()
    {
        searchStr = GUILayout.TextField(searchStr, EditorStyles.toolbarSearchField);
    }
}
